package Week10;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.io.IOException;
import java.net.URL;



public class poiInOut {
	private ArrayList<POI> poiListin, poiListOut;


	public poiInOut(ArrayList<POI> poiListin) {
		this.poiListin = poiListin;
		this.poiListOut = poiListOut;
	}

	public ArrayList<POI> getPoiListin() {
		return poiListin;
	}

	public void setPoiListin(ArrayList<POI> poiListin) {
		this.poiListin = poiListin;

	}
	
	//Write array list of POIs to a user-specified binary file.
	public void writeFile(ArrayList<POI> poiListin, File selectedFile){
		try{
			FileOutputStream fos = new FileOutputStream(selectedFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(poiListin);
			oos.flush();
			oos.close();
		}
		catch(IOException e){
			e.printStackTrace();
		}


	}
	
	//Read array list of POIs from a user-specified binary file.
	public ArrayList<POI> readFile(File selectedFile){
		try{
			FileInputStream fis = new FileInputStream(selectedFile);
			ObjectInputStream ois = new ObjectInputStream(fis);
			poiListOut = (ArrayList<POI>) ois.readObject();
			ois.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return poiListOut;

	}
	
	//Read array list of POIs from the web
	public ArrayList<POI> readFileWeb(){
		try{
			URL poiUrl = new URL("https://sites.google.com/site/myintrotogis/home/my-file-cabinet/Week10-POIs.bif");
			ObjectInputStream ois = new ObjectInputStream((InputStream) poiUrl.getContent());
			poiListOut = (ArrayList<POI>) ois.readObject();
			ois.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return poiListOut;

	}
}